/**
 * -----------------------------------
 * Copyright (c) 2021-2025
 * All rights reserved, Designed By www.linfengtech.cn
 * 林风社交论坛商业版本请务必保留此注释头信息
 * 商业版授权联系技术客服	 QQ:  3582996245
 * 严禁分享、盗用、转卖源码或非法牟利！
 * 版权所有 ，侵权必究！
 * -----------------------------------
 */
package io.linfeng.modules.app.controller;

import io.linfeng.common.annotation.NoRepeatSubmit;
import io.linfeng.common.utils.*;
import io.linfeng.common.vo.TopicDetailResponse;
import io.linfeng.common.validator.ValidatorUtils;
import io.linfeng.modules.admin.entity.AppUserEntity;
import io.linfeng.modules.admin.entity.CategoryEntity;
import io.linfeng.modules.admin.entity.TopicEntity;
import io.linfeng.modules.admin.service.AppUserService;
import io.linfeng.modules.admin.service.CategoryService;
import io.linfeng.modules.admin.service.TopicService;
import io.linfeng.modules.app.annotation.Login;
import io.linfeng.modules.app.annotation.LoginUser;
import io.linfeng.modules.app.param.*;
import io.linfeng.modules.app.service.TopicApplyService;
import io.linfeng.modules.sys.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * App端 圈子接口
 * @author linfeng
 * @email 2445465217@qq.com
 * @date 2022-01-21 17:01:12
 */
@RestController
@RequestMapping("app/topic")
@Api(tags = "移动端——圈子接口")
public class AppTopicController {


    @Autowired
    private TopicService topicService;
    @Autowired
    private AppUserService appUserService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SysConfigService configService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private TopicApplyService topicApplyService;


    @GetMapping("/list")
    @ApiOperation("圈子列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.CLASSID, value = "分类id", paramType = "query",required = true, dataType="String")
    })
    public R list(@ApiIgnore @RequestParam Map<String, Object> params){

        AppPageUtils page = topicService.queryByPageList(params);
        return R.ok().put("result", page);
    }


    /**
     * 创建圈子
     */
    @Login
    @PostMapping("/topicAdd")
    @ApiOperation("创建圈子")
    @NoRepeatSubmit
    public Result save(@RequestBody TopicAddForm topic,@ApiIgnore @LoginUser AppUserEntity user){
        ValidatorUtils.validateEntity(topic);
        Integer id = topicService.topicAdd(topic, user);
        if(id!=0){
            return new Result<>().ok(id);
        }
        return new Result().error("创建圈子失败");
    }

    /**
     * 编辑圈子
     */
    @Login
    @PostMapping("/topicEdit")
    @ApiOperation("编辑圈子")
    public Result topicEdit(@RequestBody TopicUpdateForm topic,@ApiIgnore @LoginUser AppUserEntity user){
        ValidatorUtils.validateEntity(topic);
        Boolean update = topicService.topicEdit(topic, user);
        if(update){
            redisUtils.delete(ConfigConstant.H5_TOPIC_POSTER_KEY + topic.getId());
            redisUtils.delete(ConfigConstant.WX_TOPIC_POSTER_KEY + topic.getId());
            return new Result();
        }
        return new Result().error();
    }


    /**
     * 圈子详情
     */
    @ApiOperation("圈子详情")
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "圈子id",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<TopicDetailResponse> detail(@RequestParam("id")Integer id){

        TopicDetailResponse topic=topicService.detail(id);
        return new Result<TopicDetailResponse>().ok(topic);
    }

    @ApiOperation("圈子用户列表分页")
    @PostMapping("/user")
    @Login
    public Result<AppPageUtils> user(@RequestBody TopicUserForm form,@ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils page =appUserService.findTopicUserPage(form,user);

        return new Result<AppPageUtils>().ok(page);
    }

    @ApiOperation("圈子成员管理-用户列表分页")
    @PostMapping("/userList")
    @Login
    public Result<AppPageUtils> userList(@RequestBody TopicUserForm form,@ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils page =appUserService.findUserPageByTopicAdmin(form,user);

        return new Result<AppPageUtils>().ok(page);
    }


    @ApiOperation("圈子黑名单用户管理")
    @PostMapping("/blockUserList")
    @Login
    public Result<AppPageUtils> blockUserList(@RequestBody TopicUserForm form,@ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils page =appUserService.blockUserList(form,user);

        return new Result<AppPageUtils>().ok(page);
    }

    @Login
    @GetMapping("/joinTopic")
    @ApiOperation("加入圈子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "圈子id",paramType = "query", dataType = "Integer", required = true)
    })
    public R joinTopic(@RequestParam("id")Integer id,
                       @ApiIgnore @LoginUser AppUserEntity user){

        Integer result = topicService.joinTopic(id, user);

        return R.ok().put("result",result);
    }

    @ApiOperation("退出圈子")
    @GetMapping("/userTopicDel")
    @Login
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "圈子id",paramType = "query", dataType = "Integer", required = true)
    })
    public Result userTopicDel(@RequestParam("id")Integer id,
                               @ApiIgnore @LoginUser AppUserEntity user){

        topicService.userTopicDel(id,user);
        return new Result();
    }

    @ApiOperation("圈主解散圈子")
    @GetMapping("/topicDel")
    @Login
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "圈子id",paramType = "query", dataType = "Integer", required = true)
    })
    public Result topicDel(@RequestParam("id")Integer id,
                           @ApiIgnore @LoginUser AppUserEntity user){

        topicService.topicDel(id,user);
        return new Result();
    }


    @ApiOperation("查找用户加入的圈子")
    @PostMapping("/userJoinTopic")
    @Login
    public Result<AppPageUtils> userJoinTopic(@RequestBody UserJoinTopicForm request,
                                              @ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils page=topicService.userJoinTopic(request,user);
        return new Result<AppPageUtils>().ok(page);
    }


    @ApiOperation("用户进圈申请提交")
    @PostMapping("/joinTopicApply")
    @Login
    public Result joinTopicApply(@RequestBody UserJoinTopicApplyForm request,
                                              @ApiIgnore @LoginUser AppUserEntity user){

        topicService.joinTopicApply(request,user);
        return new Result<>().ok();
    }

    @ApiOperation("热门圈子")
    @GetMapping("/hot")
    public Result<List<TopicEntity>> hot(){

        List<TopicEntity> list=topicService.hotTopic();
        return new Result<List<TopicEntity>>().ok(list);
    }

    @ApiOperation("公告内容")
    @GetMapping("/notice")
    public Result<List<String>> notice(){
        String value = configService.getValue(Constant.NOTICE_CONTENT);
        List<String> list=new ArrayList<>();
        list.add(value);
        return new Result<List<String>>().ok(list);
    }


    @ApiOperation("圈子列表分页展示并带三张图")
    @GetMapping("/classTopicAreImg")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "分类id",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<AppPageUtils> classTopicAreImg(@RequestParam("classId")Integer classId,
                              @RequestParam("page")Integer page){

        AppPageUtils pages=topicService.classTopicAreImg(classId,page);
        return new Result<AppPageUtils>().ok(pages);
    }


    @ApiOperation("用户自己的圈子列表")
    @Login
    @GetMapping("/myCreateTopic")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true)
    })
    public Result myCreateTopic(@RequestParam("page")Integer page,
                           @ApiIgnore @LoginUser AppUserEntity user){

        AppPageUtils pages=topicService.myCreateTopic(page,user);
        return new Result<AppPageUtils>().ok(pages);
    }

    @ApiOperation("查找分类")
    @PostMapping("/classList")
    @Login
    public Result<List<CategoryEntity>> classList(){

        List<CategoryEntity> list = categoryService.list();
        return new Result<List<CategoryEntity>>().ok(list);
    }

    @Login
    @GetMapping("/search")
    @ApiOperation("搜索圈子")
    @NoRepeatSubmit(lockTime = 3000)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "keyword", value = "搜索关键词",paramType = "query")
    })
    public Result<AppPageUtils> search(@RequestParam("page") Integer page,
                                       @RequestParam("keyword") String keyword){

        AppPageUtils pages =topicService.search(page,keyword);
        return new Result<AppPageUtils>().ok(pages);
    }


    @Login
    @GetMapping("/detection")
    @ApiOperation("检测用户是否可以创建圈子")
    public Result detection(@ApiIgnore @LoginUser AppUserEntity user){
        boolean result=topicService.detection(user.getUid());
        return new Result<>().ok(result);
    }

    @Login
    @GetMapping("/qrCode")
    @ApiOperation("分享二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topicId", value = "圈子id",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "origin", value = "来源",paramType = "query",  required = true),
            @ApiImplicitParam(name = "url", value = "url分享路径",paramType = "query",  required = true)
    })
    public R qrCode(@RequestParam("topicId") Integer topicId,
                    @RequestParam("origin") String origin,
                    @RequestParam("url") String url,
                    @ApiIgnore @LoginUser AppUserEntity user) throws Exception {


        String resultUrl = topicService.getQrCode(topicId,origin,url,user);
        return R.ok().put("result",resultUrl);
    }

    @Login
    @PostMapping("/giveTopic")
    @ApiOperation("设置圈子管理员")
    public Result giveTopic(@RequestBody SetAdminForm request, @ApiIgnore @LoginUser AppUserEntity user){
        Boolean result=topicService.giveTopic(request,user);
        if(!result){
            return new Result().error("转让失败");
        }
        return new Result();
    }

    @Login
    @PostMapping("/doBlock")
    @ApiOperation("圈子管理员拉黑圈子用户")
    public Result doBlock(@RequestBody BlockForm param,
                           @ApiIgnore @LoginUser AppUserEntity user){

        topicService.doBlock(param,user);
        return new Result();
    }


    @Login
    @PostMapping("/removeBlock")
    @ApiOperation("圈子管理员解除黑名单")
    public Result removeBlock(@RequestBody BlockForm param,
                           @ApiIgnore @LoginUser AppUserEntity user){

        topicService.removeBlock(param,user);
        return new Result();
    }

    @Login
    @PostMapping("/applyInfoList")
    @ApiOperation("用户入圈申请分页查询")
    public Result<AppPageUtils> applyInfoList(@RequestBody ApplyListForm request,@ApiIgnore @LoginUser AppUserEntity user){
        AppPageUtils page=topicApplyService.applyInfoList(request,user);
        return new Result<AppPageUtils>().ok(page);
    }


    @Login
    @PostMapping("/agreeApply")
    @ApiOperation("圈子管理员审核进圈用户-通过")
    public Result agreeApply(@RequestBody ApplyForm param,
                          @ApiIgnore @LoginUser AppUserEntity user){

        topicApplyService.agreeApply(param,user);
        return new Result();
    }

    @Login
    @PostMapping("/rejectApply")
    @ApiOperation("圈子管理员审核进圈用户-拒绝")
    public Result rejectApply(@RequestBody ApplyForm param,
                          @ApiIgnore @LoginUser AppUserEntity user){

        topicApplyService.rejectApply(param,user);
        return new Result();
    }

    @ApiOperation("用户端——查询密圈子创建入口开关")
    @GetMapping("/privateCirclesOpen")
    public Result privateCirclesOpen(){
        boolean result = topicService.privateCirclesOpen();

        return new Result<>().ok(result);
    }


    @Login
    @GetMapping("/checkTopicApplyByAdmin")
    @ApiOperation("查询是否有圈内管理员待处理的申请消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topicId", value = "圈子id",paramType = "query", dataType = "Integer", required = true)
    })
    public R checkTopicApplyByAdmin(@RequestParam("topicId") Integer topicId,
                              @ApiIgnore @LoginUser AppUserEntity user){

        Integer num = topicApplyService.checkTopicApplyByAdmin(topicId,user);
        return R.ok().put("result",num);
    }

}
